﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.tobyfreeman.utils {

    public class DatesTimes {

        public static const MILLISECS_PER_MIN:int = 60000;
        public static const SECS_PER_HOUR:int = 3600;
        public static const MILLISECS_PER_DAY:int = 86400000;
        public static const SECS_PER_MIN:int = 60;
        public static const MILLISECS_PER_HOUR:int = 3600000;
        public static const SECS_PER_DAY:int = 86400;

        public function DatesTimes(){
            super();
            throw (new Error("ERROR com.tobyfreeman.utils.DatesTimes constructor: Class never intended to be instantiated"));
        }
        public static function formatSecsAs_SS(secs:Number, separator:String=""):String{
            secs = Math.round(secs);
            return (((secs)<10) ? ("0" + secs) : String(secs));
        }
        public static function formatSecsAs_HH_MM_SS(secs:Number, separator:String=":"):String{
            secs = Math.round(secs);
            var hours:Number = Math.floor((secs / SECS_PER_HOUR));
            secs = (secs - (hours * SECS_PER_HOUR));
            var mins:int = Math.floor((secs / SECS_PER_MIN));
            secs = (secs - (mins * SECS_PER_MIN));
            var hours_str:String = ((hours)<10) ? ("0" + hours) : String(hours);
            var mins_str:String = ((mins)<10) ? ("0" + mins) : String(mins);
            var secs_str:String = ((secs)<10) ? ("0" + secs) : String(secs);
            return (((((hours_str + separator) + mins_str) + separator) + secs_str));
        }
        public static function formatSecsAs_MM_SS(secs:Number, separator:String=":"):String{
            secs = Math.round(secs);
            var mins:int = Math.floor((secs / SECS_PER_MIN));
            secs = (secs - (mins * SECS_PER_MIN));
            var mins_str:String = ((mins)<10) ? ("0" + mins) : String(mins);
            var secs_str:String = ((secs)<10) ? ("0" + secs) : String(secs);
            return (((mins_str + separator) + secs_str));
        }
        public static function formatSecsAs_D_HH_MM_SS(secs:Number, separator:String=":", dayDigits:int=1, dayText:String=null, daysText:String=null):String{
            secs = Math.round(secs);
            var days:Number = Math.floor((secs / SECS_PER_DAY));
            secs = (secs - (days * SECS_PER_DAY));
            var hours:Number = Math.floor((secs / SECS_PER_HOUR));
            secs = (secs - (hours * SECS_PER_HOUR));
            var mins:int = Math.floor((secs / SECS_PER_MIN));
            secs = (secs - (mins * SECS_PER_MIN));
            var daysUnits:String = ((days)==1) ? dayText : daysText;
            if (daysUnits == null){
                daysUnits = separator;
            };
            var days_str:String = String(days);
            while (days_str.length < dayDigits) {
                days_str = ("0" + days_str);
            };
            var hours_str:String = ((hours)<10) ? ("0" + hours) : String(hours);
            var mins_str:String = ((mins)<10) ? ("0" + mins) : String(mins);
            var secs_str:String = ((secs)<10) ? ("0" + secs) : String(secs);
            return (((((((days_str + daysUnits) + hours_str) + separator) + mins_str) + separator) + secs_str));
        }
        public static function formatMillisecsAs_SS_MMM(millisecs:Number, separator:String=":"):String{
            millisecs = Math.round(millisecs);
            var secs:int = Math.floor((millisecs / 1000));
            millisecs = (millisecs - (secs * 1000));
            var secs_str:String = ((secs)<10) ? ("0" + secs) : String(secs);
            var millisecs_str:String = ((millisecs)<10) ? ("00" + millisecs) : ((millisecs < 100)) ? ("0" + millisecs) : String(millisecs);
            return (((secs_str + separator) + millisecs_str));
        }
        public static function formatMillisecsAs_MM_SS_MMM(millisecs:Number, separator:String=":"):String{
            millisecs = Math.round(millisecs);
            var mins:int = Math.floor((millisecs / MILLISECS_PER_MIN));
            millisecs = (millisecs - (mins * MILLISECS_PER_MIN));
            var secs:int = Math.floor((millisecs / 1000));
            millisecs = (millisecs - (secs * 1000));
            var mins_str:String = ((mins)<10) ? ("0" + mins) : String(mins);
            var secs_str:String = ((secs)<10) ? ("0" + secs) : String(secs);
            var millisecs_str:String = ((millisecs)<10) ? ("00" + millisecs) : ((millisecs < 100)) ? ("0" + millisecs) : String(millisecs);
            return (((((mins_str + separator) + secs_str) + separator) + millisecs_str));
        }
        public static function formatMillisecsAs_HH_MM_SS_MMM(millisecs:Number, separator:String=":"):String{
            millisecs = Math.round(millisecs);
            var hours:Number = Math.floor((millisecs / MILLISECS_PER_HOUR));
            millisecs = (millisecs - (hours * MILLISECS_PER_HOUR));
            var mins:int = Math.floor((millisecs / MILLISECS_PER_MIN));
            millisecs = (millisecs - (mins * MILLISECS_PER_MIN));
            var secs:int = Math.floor((millisecs / 1000));
            millisecs = (millisecs - (secs * 1000));
            var hours_str:String = ((hours)<10) ? ("0" + hours) : String(hours);
            var mins_str:String = ((mins)<10) ? ("0" + mins) : String(mins);
            var secs_str:String = ((secs)<10) ? ("0" + secs) : String(secs);
            var millisecs_str:String = ((millisecs)<10) ? ("00" + millisecs) : ((millisecs < 100)) ? ("0" + millisecs) : String(millisecs);
            return (((((((hours_str + separator) + mins_str) + separator) + secs_str) + separator) + millisecs_str));
        }
        public static function parseMillisecondsFrom_HH_MM_SS_MMM(str:String, separator:String=":"):int{
            var parts:Array = str.split(separator);
            var millisecs:int = parseInt(parts[3]);
            millisecs = (millisecs + (1000 * parseInt(parts[2])));
            millisecs = (millisecs + (MILLISECS_PER_MIN * parseInt(parts[1])));
            millisecs = (millisecs + (MILLISECS_PER_HOUR * parseInt(parts[0])));
            return (millisecs);
        }
        public static function getDaysUntilDate(oToDate:Date, oFromDate:Date=null, roundUp:Boolean=true):Number{
            if (oFromDate == null){
                oFromDate = new Date();
            };
            var days:Number = ((oToDate.getTime() - oFromDate.getTime()) / MILLISECS_PER_DAY);
            if (roundUp){
                return (Math.ceil(days));
            };
            return (days);
        }

    }
}//package com.tobyfreeman.utils 
